﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
using WeChatSDK.Common;
using WeChatSDK.Common.Enums.Http;
using WeChatSDK.RequestComponent.Http;
using WeChatSDK.WeChatDto.Button;
using WeChatSDK.WeChatDto.Request.Http;
using WeChatSDK.WeChatServiceComponent.Conversation.BaseSupport;

namespace WeChatSDK.WeChatServiceComponent.Button
{
    /// <summary>
    /// 微信自定义菜单服务
    /// </summary>
    public class ButtonService
    {
        private readonly HttpHandle httpHandle;

        public ButtonService()
        {
            this.httpHandle = new HttpHandle();
        }
        /// <summary>
        /// 创建自定义菜单
        /// </summary>
        /// <param name="buttonDto">自定义菜单按钮</param>
        /// <returns>{"errcode":0,"errmsg":"ok"}</returns>
        public HttpResponseDto CreateCostomMenu(ButtonDto buttonDto)
        {
            string url = string.Format("https://api.weixin.qq.com/cgi-bin/menu/create?access_token={0}", new BaseSupportService().CreateAccessToken().AccessToken);
            HttpRequestDto httpRequestDto = new HttpRequestDto();
            httpRequestDto.Url = url;
            httpRequestDto.Headers.Add("Content-Type", "application/json");
            httpRequestDto.Content = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(buttonDto, WeChatGeneral.JsonSerializerSettings));
            httpRequestDto.Method = MethodType.POST;
            httpRequestDto.ContentType = FormatType.JSON;
            return httpHandle.WebSendRequest(httpRequestDto);
        }
        /// <summary>
        /// 查询自定义菜单
        /// </summary>
        /// <returns>请查阅微信官网文档</returns>
        public HttpResponseDto GetCostomMenu()
        {
            string url = string.Format("https://api.weixin.qq.com/cgi-bin/menu/get?access_token={0}", new BaseSupportService().CreateAccessToken().AccessToken);
            HttpRequestDto httpRequestDto = new HttpRequestDto();
            httpRequestDto.Url = url;
            httpRequestDto.Headers.Add("Content-Type", "application/json");
            httpRequestDto.Method = MethodType.GET;
            httpRequestDto.ContentType = FormatType.JSON;
            return httpHandle.WebSendRequest(httpRequestDto);
        }
        /// <summary>
        /// 获取自定义菜单配置
        /// </summary>
        /// <returns>请查阅微信官网文档</returns>
        public HttpResponseDto GetCurrentSelfMenuInfo()
        {
            string url = string.Format("https://api.weixin.qq.com/cgi-bin/get_current_selfmenu_info?access_token={0}", new BaseSupportService().CreateAccessToken().AccessToken);
            HttpRequestDto httpRequestDto = new HttpRequestDto();
            httpRequestDto.Url = url;
            httpRequestDto.Headers.Add("Content-Type", "application/json");
            httpRequestDto.Method = MethodType.GET;
            httpRequestDto.ContentType = FormatType.JSON;
            return httpHandle.WebSendRequest(httpRequestDto);
        }
        /// <summary>
        /// 删除自定义菜单
        /// </summary>
        /// <returns>{"errcode":0,"errmsg":"ok"}</returns>
        public HttpResponseDto DeleteCostomMenu()
        {
            string url = string.Format("https://api.weixin.qq.com/cgi-bin/menu/delete?access_token={0}", new BaseSupportService().CreateAccessToken().AccessToken);
            HttpRequestDto httpRequestDto = new HttpRequestDto();
            httpRequestDto.Url = url;
            httpRequestDto.Headers.Add("Content-Type", "application/json");
            httpRequestDto.Method = MethodType.GET;
            httpRequestDto.ContentType = FormatType.JSON;
            return httpHandle.WebSendRequest(httpRequestDto);
        }
    }
}
